package cn.iocoder.devlop.module.annual.report.forguaranteeinfo.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.common.service.EnttypeEntclassifyService;
import cn.iocoder.devlop.module.annual.report.forguaranteeinfo.controller.vo.ForguaranteeinfoCreateReqVO;
import cn.iocoder.devlop.module.annual.report.forguaranteeinfo.controller.vo.ForguaranteeinfoOverwriteVO;
import cn.iocoder.devlop.module.annual.report.forguaranteeinfo.controller.vo.ForguaranteeinfoUpdateReqVO;
import cn.iocoder.devlop.module.annual.report.forguaranteeinfo.convert.ForguaranteeinfoConvert;
import cn.iocoder.devlop.module.annual.report.forguaranteeinfo.dal.ForguaranteeinfoMapper;
import cn.iocoder.devlop.module.annual.report.forguaranteeinfo.dal.dataobject.ForguaranteeinfoDO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.dal.dataobject.MarpripinfoDO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.service.MarpripinfoService;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ObjectComparisonUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.devlop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.devlop.module.annual.report.enums.ErrorCodeConstants.FORGUARANTEEINFO_NOT_EXISTS;

/**
 * 企业年报对外提供保证担保信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ForguaranteeinfoServiceImpl implements ForguaranteeinfoService {

    @Resource
    private ForguaranteeinfoMapper forguaranteeinfoMapper;

    @Resource
    private BaseinfoService baseinfoService;


    @Resource
    private MarpripinfoService marpripinfoService;

    @Resource
    private EnttypeEntclassifyService enttypeEntclassifyService;

    @Override
    public String createForguaranteeinfo(ForguaranteeinfoCreateReqVO createReqVO) {
        // 插入
        ForguaranteeinfoDO forguaranteeinfo = ForguaranteeinfoConvert.INSTANCE.convert(createReqVO);
        forguaranteeinfoMapper.insert(forguaranteeinfo);
        // 返回
        return forguaranteeinfo.getMoreid();
    }

    @Override
    public void updateForguaranteeinfo(ForguaranteeinfoUpdateReqVO updateReqVO) {
        // 校验存在
        validateForguaranteeinfoExists(updateReqVO.getMoreid());
        // 更新
        ForguaranteeinfoDO updateObj = ForguaranteeinfoConvert.INSTANCE.convert(updateReqVO);
        forguaranteeinfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteForguaranteeinfo(String id) {
        // 校验存在
        validateForguaranteeinfoExists(id);
        // 删除
        forguaranteeinfoMapper.deleteById(id);
    }

    private void validateForguaranteeinfoExists(String id) {
        if (forguaranteeinfoMapper.selectById(id) == null) {
            throw exception(FORGUARANTEEINFO_NOT_EXISTS);
        }
    }

    @Override
    public ForguaranteeinfoDO getForguaranteeinfo(String id) {
        return forguaranteeinfoMapper.selectById(id);
    }




    @Override
    public List<ForguaranteeinfoDO> getForguaranteeinfoList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return forguaranteeinfoMapper.selectListByAncheid(ancheid);
    }

    @Override
    public void deleteForguaranteeinfoAll(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        LambdaQueryWrapperX<ForguaranteeinfoDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(ForguaranteeinfoDO::getAncheid,ancheid);
        forguaranteeinfoMapper.delete(wrapperX);
    }

    @Override
    public void createForguaranteeinfoByList(String regno, String ancheyear, List<ForguaranteeinfoCreateReqVO> forguaranteeinfoList) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        MarpripinfoDO marpripinfo = marpripinfoService.getMarpripinfo(regno);
        String entclassify = enttypeEntclassifyService.getEntclassify(marpripinfo.getEnttype());
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(regno, ancheyear);
        if (null == baseinfo){
            baseinfoService.initBaseinfo(regno,ancheyear);
        }
        BaseinfoDO baseinfoDO = new BaseinfoDO();
        baseinfoDO.setGuaranteesign("2");
        baseinfoDO.setAncheid(IdGenerateUtil.getAncheid(regno, ancheyear));
        if (CollUtil.isNotEmpty(forguaranteeinfoList)) {
            List<ForguaranteeinfoDO> forguaranteeinfoDOS = ForguaranteeinfoConvert.INSTANCE.convertList01(forguaranteeinfoList);
            for (ForguaranteeinfoDO forguaranteeinfo : forguaranteeinfoDOS) {
                forguaranteeinfo.setEntclassify(entclassify);
                forguaranteeinfo.setAncheid(ancheid);
                forguaranteeinfo.setMoreid(IdUtil.simpleUUID());
            }
            baseinfoDO.setGuaranteesign("1");
            forguaranteeinfoMapper.insertBatch(forguaranteeinfoDOS);
        }
        baseinfoService.baseinfoUpdate(baseinfoDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteForguaranteeinfo(ForguaranteeinfoOverwriteVO updateReqVO) {
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        if (ObjUtil.isNotEmpty(baseinfo)){
            if (StrUtil.equals(baseinfo.getState(),"02") ||StrUtil.equals(baseinfo.getState(),"03")) {
                List<ForguaranteeinfoDO> oldForguaranteeinfoList = getForguaranteeinfoList(updateReqVO.getRegno(), updateReqVO.getAncheyear());
                List<ForguaranteeinfoCreateReqVO> newAlterstockinfoList = updateReqVO.getForguaranteeinfoList();
                List<ForguaranteeinfoCreateReqVO> oldreqs = ForguaranteeinfoConvert.INSTANCE.convertList3(oldForguaranteeinfoList);
                ObjectComparisonUtil.compareObjectList(oldreqs,newAlterstockinfoList,IdGenerateUtil.getAncheid(updateReqVO.getRegno(), updateReqVO.getAncheyear()),"股权变更");
            }
        }
        deleteForguaranteeinfoAll(updateReqVO.getRegno(),updateReqVO.getAncheyear());
        createForguaranteeinfoByList(updateReqVO.getRegno(),updateReqVO.getAncheyear(),updateReqVO.getForguaranteeinfoList());
        baseinfoService.updateState(updateReqVO.getRegno(),updateReqVO.getAncheyear());
    }

}
